//
//  PwrdSdkApi.cpp
//  clientOversea
//
//  Created by ming on 2019/1/9.
//

#include "PwrdSdkApi.hpp"
#import "ChannelAPI_Pwrd.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"

static std::string s_versionInfo = "1.0.0.1";

void PwrdSdkApi::setRemoteVersionInfo(std::string versionInfoStr){
    s_versionInfo = versionInfoStr;
}

std::string PwrdSdkApi::getRemoteVersionInfo(){
    return s_versionInfo;
}

std::string PwrdSdkApi::getRemoteAppVersion(){
    auto idx = s_versionInfo.find_last_of(".");
    std::string ret = s_versionInfo.substr(0, idx);
    return ret;
}

std::string PwrdSdkApi::getRemoteResourceVersion(){
    auto idx = s_versionInfo.find_last_of(".");
    std::string ret = s_versionInfo.substr(idx+1);
    return ret;
}

void PwrdSdkApi::jumpToAppShop(){
   [ChannelAPI jumpToAppShop];
}

bool PwrdSdkApi::isReview(){
    return (bool)[ChannelAPI isReview];
}

bool PwrdSdkApi::checkPermissions(){
    return true;
}
void PwrdSdkApi::requestPermissions(){
//    cocos2d::EventCustom customEvent("CHANNEL_REQUEST_PREMISSIONS_EVENT");
//    std::string param_ = "success";
//    customEvent.setUserStr(param_);
//    cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&customEvent);
}

void PwrdSdkApi::goAppSetting(){
    
}

std::vector<std::string> PwrdSdkApi::getCdnList(){
    std::vector<std::string> ret;
    cocos2d::Configuration* configuration = cocos2d::Configuration::getInstance();
    const cocos2d::Value& cdnUrlValue = configuration->getValue("CDNUpdateUrl");
    std::string strCdn = cdnUrlValue.asString();
    while (true) {
        auto index = strCdn.find(";");
        std::string subStr = strCdn.substr(0, index);
        if (!subStr.empty()) {
            // Append automatically "/"
            if (subStr[subStr.size() - 1] != '/') {
                subStr.append("/");
            }
            ret.push_back(subStr);
        }
        strCdn.erase(0, index + 1);
        if (index == -1) {
            break;
        }
    }
    return ret;
}

// data = 事件名
void PwrdSdkApi::wanmeiTrackEvent(const char* data){
    if(data != nullptr){
        rapidjson::StringBuffer s;
        rapidjson::Writer<rapidjson::StringBuffer> writer(s);
        writer.StartObject();
        writer.Key("eventName");
        writer.String(data);
        writer.EndObject();
        std::string jsonStr = s.GetString();
        NSDictionary * dict = @{@"jsonStr":[NSString stringWithUTF8String:jsonStr.c_str()]};
        [ChannelAPI wanmeiTrackEvent:dict];
    }
}

void PwrdSdkApi::wanmeiGameEvent(const char* eventName, int stateIndex, const char* url, const char* errorMsg){
    rapidjson::StringBuffer s;
    rapidjson::Writer<rapidjson::StringBuffer> writer(s);
    writer.StartObject();
    writer.Key("eventName");
    writer.String(eventName);
    writer.Key("stateIndex");
    writer.Int(stateIndex);
    writer.Key("url");
    writer.String(url);
    writer.Key("errorMsg");
    writer.String(errorMsg);
    writer.EndObject();
    std::string jsonStr = s.GetString();
    NSDictionary * dict = @{@"jsonStr":[NSString stringWithUTF8String:jsonStr.c_str()]};
    [ChannelAPI wanmeiGameEvent:dict];
}

void PwrdSdkApi::setSDKLanguage(const char* language){
    [ChannelAPI setSDKLanguage:[NSString stringWithUTF8String:language]];
}
